import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


     
    import javax.swing.*;
import javax.swing.border.TitledBorder;
     
    public class guiReworked implements ActionListener {
        public static JTextArea textArea = new JTextArea("Rickard");
        static JTextArea textOffer = new JTextArea("FEKI BEDRU!");
     
        private static final String SEARCH = "Search";
        private static final String OFFERS = "Offers";
        private static final String LOGIN = "Log in";
        private JPanel cardHolder;
        private JButton homeButton = new JButton("Search");
        private JButton blueButton = new JButton("Offers");
        private JButton redButton = new JButton("Log in");
       
        //LoginScreen
        private JButton loginScreenLogin = new JButton("Log in");
        private JButton loginScreenCancel = new JButton("Cancel");
        private JTextField loginScreenUserField = new JTextField();
        private JPasswordField loginScreenPasswordField = new JPasswordField();  
        public static JLabel admin = new JLabel("User");
        public static JLabel password = new JLabel("Password");
           
        //SearchScreen
        private JButton Search = new JButton("Search");
        private JButton Reset = new JButton("Reset");
       
        JRadioButton sortByPrice = new JRadioButton("price");
        JRadioButton sortByFood = new JRadioButton("food type");
        JRadioButton sortByRestaurant = new JRadioButton("restaurant");
       
     
            JComboBox restaurantList;
            JComboBox foodList;
            JComboBox priceList;
     
        public JPanel createCardHolderPanel() {
            cardHolder = new JPanel(new CardLayout());
            cardHolder.add(searchPage(), SEARCH);
            cardHolder.add(specialOffers(), OFFERS);
            cardHolder.add(LoginBox(), LOGIN);
           
            return cardHolder;
        }
       
    //    private JPanel createColorPanel(Color color) {
    //        JPanel panel = new JPanel();
    //        panel.add(LoginBox());
    //        panel.setBackground(color);
    //        return panel;
    //    }
        public JPanel searchPage()  {
            JPanel searchPage = new JPanel();
            searchPage.add(searchArea());
            return searchPage;
        }
        
        /* Code by feki */
        public JPanel specialOffers()       {
            JPanel specialOffers = new JPanel();
            specialOffers.setBackground(Color.red);
            specialOffers.add(textOffer);
            return specialOffers;
        }
        /* End Code by feki */
       
        private void reset(GridBagConstraints con) {
            //  default constraints
            con.gridx = GridBagConstraints.RELATIVE;
            con.gridy = GridBagConstraints.RELATIVE;
            con.gridwidth = 1;
            con.gridheight = 1;
            con.weightx = 0;
            con.weighty = 0;
            con.anchor = GridBagConstraints.CENTER;
            con.fill = GridBagConstraints.NONE;
            con.insets = new Insets(0, 0, 0, 0);
            con.ipadx = 0;
            con.ipady = 0;
          }
        public JPanel searchArea()  {
            String[] restaurantStrings =    {"", "Chinese", "Italian", "Swedish"};
            restaurantList = new JComboBox(restaurantStrings);
            restaurantList.addActionListener(this);
            restaurantList.setSelectedIndex(2);
           
            String[] foodStrings = {"", "Meat", "Fish", "Vegetarian"};
            foodList = new JComboBox(foodStrings);
            foodList.addActionListener(this);
            foodList.setSelectedIndex(2);
     
     
            String[] priceStrings = {"", "50-100kr", "101-150kr", "151-200kr", "201+ kr"};
            priceList = new JComboBox(priceStrings);
            priceList.addActionListener(this);
            priceList.setSelectedIndex(1);
     
     
            JLabel empty = new JLabel();
            JLabel empty2 = new JLabel();
           JLabel empty3 = new JLabel();
           
            JLabel TypeOfRest = new JLabel("Restaurant");
            JLabel TypeOfFood = new JLabel("Food");
            JLabel TypeOfPrice = new JLabel("Price");
           
            JButton sample = new JButton("sa1");
           JButton sample2 = new JButton("sa2");
          JButton sample3 = new JButton("sa3");
     
           
            JPanel se = new JPanel(new GridBagLayout());
                    GridBagConstraints c = new GridBagConstraints();
                    //Type of restaurant
                    c.gridx = 0;
                    c.gridy = 0;
                    c.ipadx = 10;
            c.anchor = GridBagConstraints.SOUTH;
           
            se.add(TypeOfRest, c);
                    c.gridx = 0;
                    c.gridy = 1;
                    c.ipadx = 30;
            se.add(restaurantList, c);
           
            //emptyspace
            reset(c);
            c.gridx = 1;
                    c.gridy = 0;
                    c.ipady = 30;
                    c.ipadx = 20;
                    c.gridheight = 1;
            se.add(new JLabel(), c);
           
            //type of food
            reset(c);
            c.gridx = 2;
                    c.gridy = 0;
            c.anchor = GridBagConstraints.SOUTH;
                    c.ipadx = 10;
                    se.add(TypeOfFood, c);
                    reset(c);
            c.gridx = 2;
                    c.gridy = 1;
                    c.ipadx = 10;
                    se.add(foodList, c);
                   
                    //empty space
                    reset(c);
                    c.gridx = 3;
                    c.gridy = 0;
                    c.ipady = 30;
                    c.ipadx = 20;
                    c.gridheight = 1;
            se.add(new JLabel(), c);
           
            //type of price
            reset(c);
            c.gridx = 4;
                    c.gridy = 0;
                    c.anchor = GridBagConstraints.SOUTH;
                    se.add(TypeOfPrice, c);
                   
            reset(c);
                    c.gridx = 4;
                    c.gridy = 1;
                    se.add(priceList, c);
                    reset(c);
                   
                    //empty space
                    c.gridx = 0;
                    c.gridy = 3;
                    c.gridwidth = 5;
                    c.ipadx = 300;
                    c.ipady = 100;
                    se.add(empty3, c);
                   
   
                    JPanel s1 = new JPanel();
                    s1.add(Search);
                    s1.add(Reset);
                    Search.addActionListener(this);
                    Reset.addActionListener(this);
                    JPanel s2 = new JPanel();
                    s2.add(new JLabel("Sort by: "));
                    s2.add(sortByFood);
                    s2.add(sortByPrice);
                    s2.add(sortByRestaurant);
                   
                    JPanel s3 = new JPanel(new BorderLayout());
                    s3.add(s1, BorderLayout.NORTH);
                    s3.add(s2, BorderLayout.SOUTH);
                   
                ButtonGroup sorting = new ButtonGroup();
                sorting.add(sortByFood); sorting.add(sortByPrice); sorting.add(sortByRestaurant);
                   
                   
                    se.setBorder(new TitledBorder("Search"));
                    JPanel fullSearch = new JPanel(new BorderLayout());
                    fullSearch.add(se, BorderLayout.NORTH);
                    fullSearch.add(s3);
                   
                    JPanel scrollboxx = new JPanel();
                    
                    JScrollPane scrollerBox = new JScrollPane();
                    JPanel BBB = new JPanel();
                    BBB.setLayout(new GridLayout(40,1,5,5)); 
                    BBB.add(textArea);
                    
                  
                    scrollerBox.setViewportView(BBB);
                    scrollerBox.setPreferredSize(new Dimension(300, 300));
                    //scrollerBox.add(textArea);
                    scrollboxx.add(scrollerBox);

                    scrollboxx.setPreferredSize(new Dimension(400, 400));
                    scrollboxx.setBackground(Color.black);
                    //textArea.setPreferredSize(new Dimension(50, 50));
                
                fullSearch.add(scrollboxx, BorderLayout.SOUTH);
                   
            return fullSearch;
        }
        
        /* Echo's code */
        
        public JScrollPane scrollBox()      {
            JScrollPane scrollerBox = new JScrollPane();
                    JPanel BBB = new JPanel();
                    BBB.setLayout(new GridLayout(40,1,5,5)); 
            
            for(int i = 0; i < 40; i++) {  
                 BBB.add(new JLabel("JLabel " + i)); 
                                        } 
                  scrollerBox.setViewportView(BBB);
                    scrollerBox.setPreferredSize(new Dimension(300, 300));
                    return scrollerBox;
                   
        }
        
        /* End echo code */
       
   
        public JPanel LoginBox()    {
                    JPanel loginScreen = new JPanel(new GridBagLayout());
                    GridBagConstraints c = new GridBagConstraints();
                    JLabel admin = new JLabel("User");
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.gridx = 0;
                    c.gridy = 0;
                    loginScreen.add(admin, c);
                    JLabel password = new JLabel("Password");
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.gridx = 0;
                    c.gridy = 1;
                    loginScreen.add(password, c);
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.ipadx = 70;
                    c.gridx = 2;
                    c.gridy = 0;
                    loginScreen.add(loginScreenUserField, c);
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.gridx = 2;
                    c.gridy = 1;
                    loginScreen.add(loginScreenPasswordField, c);
                    JPanel empty1 = new JPanel();
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.gridx = 1;
                    c.gridy = 0;
                    c.ipadx = 30;
                    loginScreen.add(empty1, c);
                   
                    JPanel loginOrCancel = new JPanel(new GridBagLayout());
                    GridBagConstraints b = new GridBagConstraints();
                    b.fill = GridBagConstraints.HORIZONTAL;
                    b.gridx = 0;
                    b.gridy = 0;
                    b.ipadx = 15;
                    loginOrCancel.add(loginScreenLogin, b);
                    loginScreenLogin.addActionListener(this);
                    b.fill = GridBagConstraints.HORIZONTAL;
                    b.gridx = 1;
                    b.gridy = 0;
                    b.ipadx = 15;
                    loginOrCancel.add(loginScreenCancel, b);
                    loginScreenCancel.addActionListener(this);
     
           
                    JPanel whole = new JPanel(new BorderLayout());
                    whole.setBorder(new TitledBorder("Log in"));
                    whole.add(loginScreen, BorderLayout.NORTH);
                    whole.add(loginOrCancel, BorderLayout.SOUTH);
                   
                    whole.setPreferredSize(new Dimension(200, 100));
    //              whole.setLocation(300, 200);
                   
                    JPanel re = new JPanel();
                re.setLayout(new GridBagLayout());
                re.add(whole, new GridBagConstraints());
                   
                    return re;
                    
     
        }
        public JPanel createButtonPanel() {
            JPanel buttonPanel = new JPanel(new FlowLayout());
            buttonPanel.add(homeButton);
            buttonPanel.add(blueButton);
            buttonPanel.add(redButton);
            homeButton.addActionListener(this);
            blueButton.addActionListener(this);
            redButton.addActionListener(this);
            return buttonPanel;
        }
     
        public JPanel createContentPane() {
            JPanel panel = new JPanel(new BorderLayout());
            panel.setPreferredSize(new Dimension(800, 600));
            panel.add(createButtonPanel(), BorderLayout.NORTH);
            panel.add(createCardHolderPanel(), BorderLayout.CENTER);
            return panel;
        }
     
        public JMenuBar createMenuBar() {
            JMenuBar menuBar = new JMenuBar();
            JMenu file = new JMenu("File");
            JMenu users = new JMenu("Users");
            JMenu options = new JMenu("Options");
            JMenu help = new JMenu("Help");
            menuBar.add(file);
            menuBar.add(users);
            menuBar.add(options);
            menuBar.add(help);
            return menuBar;
        }
     
        @Override
        public void actionPerformed(ActionEvent e) {
            CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
            if (e.getSource() == homeButton) {
                cardLayout.show(cardHolder, SEARCH);
            }
            if (e.getSource() == blueButton) {
                cardLayout.show(cardHolder, OFFERS);
            }
            if (e.getSource() == redButton) {
                cardLayout.show(cardHolder, LOGIN);
            }
            //searchscreen
            if (e.getSource() == Search)    {
                    String restName = (String)restaurantList.getSelectedItem();
                    String foodName = (String)foodList.getSelectedItem();
                    String priceName = (String)priceList.getSelectedItem();
                    
                    	
               
                    
       
                    search.find(restName , foodName , priceName);
                    
                    
            }
            //loginscreen
            if (e.getSource() == loginScreenLogin)  {
            /* Rickard */       
            	String user = loginScreenUserField.getText();
            	String pass = loginScreenPasswordField.getText();
            	System.out.println(user + pass);
                login.userpass(user, pass);
            
            }
            if (e.getSource() == loginScreenCancel) {
                    cardLayout.show(cardHolder, SEARCH);
            }
        }
     
        public static void createAndShowGUI() {
            JFrame frame = new JFrame("JDecider Group 13");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            guiReworked main = new guiReworked();
    //        frame.setJMenuBar(main.createMenuBar());
            frame.add(main.createContentPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setResizable(false);
            frame.setVisible(true);
        }
        
        public static void searchresult(String abba){
        	
        	textArea.setText(abba);
        System.out.println("searchresult" + abba);
        }
     
        public static void main(String[] args) {
//            try {
//                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//    //            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
//            } catch (UnsupportedLookAndFeelException ex) {
//                ex.printStackTrace();
//            } catch (IllegalAccessException ex) {
//                ex.printStackTrace();
//            } catch (InstantiationException ex) {
//                ex.printStackTrace();
//            } catch (ClassNotFoundException ex) {
//                ex.printStackTrace();
//            }
//            /* Turn off metal's use of bold fonts */
//            UIManager.put("swing.boldMetal", Boolean.FALSE);
            SwingUtilities.invokeLater(new Runnable() {
           
                @Override
                public void run() {
                     System.out.println("hej!");
                    createAndShowGUI();
                }
            });
        }
       
        
     }
